import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from "./../common/store";

import HelloWorld from '@/components/HelloWorld'
import TrainingLayoutStudent from "../pages/TrainingLayoutStudent";
import TrainingLayoutTeacher from "../pages/TrainingLayoutTeacher";
import StudentCourses from "../pages/training/courses/StudentCourses";
import Login from "../pages/training/home/Login";
import TeacherCourses from "@/pages/training/courses/TeacherCourses";
import StudentQues from "@/pages/training/question/StudentQues"
import TeacherQues from "@/pages/training/question/TeacherQues"
import AdminLayout from "@/pages/admin/AdminLayout"
import Profile from "@/pages/common/Profile"
import TeacherProgress from "@/pages/training/courses/TeacherProgress"

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: "/profile",
      name: "Profile",
      component: Profile,
    },
    {
      path: '/trainEntrance',
      name: 'TrainLogin',
      component: Login
    },
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/train/student',
      name: 'Train',
      component: TrainingLayoutStudent,
      children: [
        {
          path: 'courses',
          name: 'StudentCourses',
          component: StudentCourses
        },
        {
          path: 'question',
          name: 'StudentQuestion',
          component: StudentQues
        }
      ]
    },
    {
      path: '/train/teacher',
      name: 'Train',
      component: TrainingLayoutTeacher,
      children: [
        {
          path: 'courses',
          name: 'TeacherCourses',
          component: TeacherCourses
        },
        {
          path: 'question',
          name: 'TeacherQuestion',
          component: TeacherQues
        },
        {
          path: 'progress',
          name: 'TeacherProgress',
          component: TeacherProgress
        }
      ]
    },
    {
      name: "AdminLayout",
      path: "/admin/:type",
      component: AdminLayout,
    },
  ]
})
router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => !record.meta.noAuth)) {
    if (getToken()) {
      // 管理员
      next();
    } else {
      if (to.path === "/trainEntrance") {
        next();
      } else {
        next({ path: "/trainEntrance" });
      }
    }
  } else {
    // 如果不需要权限校验，直接进入路由页面
    next();
  }
});
export default router
